<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>Hello World</title>
  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
  <style>
    body{
      padding:5px;
    }
  </style>
</head>

<body>
  <div id="root"></div>
  <script type="text/babel">

    const data = [
      {category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football"},
      {category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball"},
      {category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball"},
      {category: "Electronics", price: "$99.99", stocked: true, name: "iPod Touch"},
      {category: "Electronics", price: "$399.99", stocked: false, name: "iPhone 5"},
      {category: "Electronics", price: "$199.99", stocked: true, name: "Nexus 7"}
    ];

    class FilterableProductTable extends React.Component{
      constructor(props){
        super(props);

        this.onKeywordChange = this.onKeywordChange.bind(this);
        this.onInStockOnlyChange = this.onInStockOnlyChange.bind(this);
        this.state = {keyword:'Base',inStockOnly:false};
      }

      onKeywordChange(e){
        this.setState({keyword:e});
      }

      onInStockOnlyChange(e){
        this.setState({inStockOnly:e});
      }

      render(){
        return (
          <div>
            <SearchBar keyword={this.state.keyword} inStockOnly={this.state.inStockOnly}
               onKeywordChange={this.onKeywordChange} onInStockOnlyChange={this.onInStockOnlyChange}/>
            <ProductTable data={this.props.data} keyword={this.state.keyword} inStockOnly={this.state.inStockOnly}/>
          </div>
        );
      }
    }

    class SearchBar extends React.Component{
      constructor(props){
        super(props);

        this.handleChange = this.handleChange.bind(this);
        this.handleCheck = this.handleCheck.bind(this);
      }

      handleCheck(e){
        this.props.onInStockOnlyChange(e.target.checked);
      }

      handleChange(e){
        this.props.onKeywordChange(e.target.value);
      }

      render(){
        return (
          <form>
            <input type="text" value={this.props.keyword} placeholder="Search..." onChange={this.handleChange}/>
            <br/>
            <label>
                <input type="checkbox" checked={this.props.inStockOnly} onChange={this.handleCheck}/>
                Only show products in stock
            </label>
          </form>
        );
      }
    }

    class ProductTable extends React.Component{
      constructor(props){
        super(props);
      }

      render(){
        const data = this.props.data;
        let rows = [];
        var lastCatagory = '';
        data.map((item)=>{
          const keyword = this.props.keyword;
          const inStockOnly = this.props.inStockOnly;

          if(item.name.indexOf(keyword) === -1){
            return;
          }

          if(inStockOnly && !item.stocked){
            return;
          }

          let category = item.category;
          if(category != lastCatagory){
            let productCategoryRow = <ProductCategoryRow text={item.category} key={item.category}/>;
            rows.push(productCategoryRow);

            lastCatagory = category;
          }

          let productRow = <ProductRow name={item.name} price={item.price} key={item.name} stocked={item.stocked}/>;
          rows.push(productRow);
        });

        return (
          <table>
            <thead>
              <tr>
                <th>Name</th>
                <th>Price</th>
              </tr>
            </thead>
            <tbody>
              {rows}
            </tbody>
          </table>
        );
      }
    }

    function ProductCategoryRow(props){
      return (
        <tr>
          <th colSpan="2">
              {props.text}
          </th>
        </tr>);
    }

    function ProductRow(props){
      const name = props.stocked ? props.name : 
        (<span style={{color:'red'}}>{props.name}</span>);
      return (
        <tr>
          <td>{name}</td>
          <td>{props.price}</td>
        </tr>
      );
    }

    ReactDOM.render(<FilterableProductTable data={data}/>,document.getElementById('root'));
  </script>
  <!--
      Note: this page is a great way to try React but it's not suitable for production.
      It slowly compiles JSX with Babel in the browser and uses a large development build of React.

      To set up a production-ready React build environment, follow these instructions:
      * https://reactjs.org/docs/add-react-to-a-new-app.html
      * https://reactjs.org/docs/add-react-to-an-existing-app.html

      You can also use React without JSX, in which case you can remove Babel:
      * https://reactjs.org/docs/react-without-jsx.html
      * https://reactjs.org/docs/cdn-links.html
    -->
</body>

</html>